﻿using AutoTask.Model;
using AutoTask.Model.DB;
using RpcClient;
using RpcHelper;
using SqlSugarHelper;

namespace AutoTask.DAL.Repository
{
    internal class AutoTaskPlanDAL : IAutoTaskPlanDAL
    {
        private IRepository<AutoTaskPlanModel> _BasicDAL;
        public AutoTaskPlanDAL(IRepository<AutoTaskPlanModel> dal)
        {
            _BasicDAL = dal;
        }
        public AutoTaskPlan[] Gets(Guid taskId)
        {
            return this._BasicDAL.Gets(a => a.TaskId == taskId && a.IsEnable, c => new AutoTaskPlan
            {
                BeginDate = c.BeginDate,
                SpaceDay = c.SpaceDay,
                SpaceType = c.SpaceType,
                SpaceWeek = c.SpaceWeek,
                SpeceNum = c.SpeceNum,
                DayBeginSpan = c.DayBeginSpan,
                DayEndSpan = c.DayEndSpan,
                DayRate = c.DayRate,
                DaySpaceNum = c.DaySpaceNum,
                DaySpaceType = c.DaySpaceType,
                DayTimeSpan = c.DayTimeSpan,
                EndDate = c.EndDate,
                ErrorId = c.ErrorCode,
                ExecRate = c.ExecRate,
                ExecSpace = c.ExecSpace,
                ExecTime = c.ExecTime,
                Id = c.Id,
                IsEnable = c.IsEnable,
                PlanTitle = c.PlanTitle,
                PlanType = c.PlanType
            });
        }

        public void StopTaskPlan(TaskPlanResult[] stops)
        {
            AutoTaskPlanModel[] models = stops.ConvertMap<TaskPlanResult, AutoTaskPlanModel>();
            if (!_BasicDAL.UpdateOnly(models, a =>new {
                a.ErrorCode,
                a.IsEnable
            }))
            {
                throw new ErrorException("task.plan.update.fail");
            }
        }
    }
}
